﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Firestarter.DB.Schema.Test.Logic;
using Firestarter.Test.Base;
using FireStarterModeller.BO;
using FireStarterModeller.Logic;
using FireStarterModeller.UI.ViewEditor;
using Habanero.Faces.Base;
using Habanero.Faces.Win;
using Habanero.Testability;
using NUnit.Framework;
using Rhino.Mocks;
using DialogResult = Habanero.Faces.Base.DialogResult;
using MessageBoxButtons = Habanero.Faces.Base.MessageBoxButtons;
using MessageBoxIcon = Habanero.Faces.Base.MessageBoxIcon;

namespace FireStarterModeller.Test.UI.ViewEditor
{
    [TestFixture]
    public class TestUIViewAddElementForm
    {
        [TestFixtureSetUp]
        public void SetupFixture()
        {
            UITestUtils.SetupFixture();
        }

        [SetUp]
        public virtual void SetupTest()
        {
            UITestUtils.SetupTest();
        }

        private static IControlFactory GetControlFactory()
        {
            ControlFactoryWin controlFactory = MockRepository.GeneratePartialMock<ControlFactoryWin>();
            controlFactory.Stub(win => win.ShowMessageBox("Are you sure you want to delete this UI View?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Stop)).Return(DialogResult.Yes);
            return controlFactory;
        }

        [Test]
        public void Test_Template()
        {
            //---------------Set up test pack-------------------
            //---------------Assert preconditions---------------
            //---------------Execute Test ----------------------
            //---------------Test Result -----------------------
        }

        [Test]
        public void Test_Construction()
        {
            //---------------Set up test pack-------------------
            //---------------Execute Test ----------------------
            IUIViewAddElementForm form = new UIViewAddElementForm(GetControlFactory(), null);
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(IFormHabanero), form);
            Assert.AreEqual(4, form.Controls.Count);
            Assert.AreSame(form.Controls[1], form.ElementsComboBox);
            Assert.AreSame(form.Controls[2], form.FieldsPanel);
            Assert.AreSame(form.Controls[3], form.Buttons);
            Assert.AreEqual(2, form.Buttons.Controls.Count);
            Assert.AreSame(form.Buttons.Controls[0], form.CancellingButton);
            Assert.AreSame(form.Buttons.Controls[1], form.AddButton);
        }

        [Test]
        public void Test_Construction_ComboBoxContainsElements()
        {
            //---------------Set up test pack-------------------
            string[] expectedContents = { "Grid Column", "Form Field", "Grid", "Form", "Grid Filter", "Grid Filter Property", "UI", "Form Tab", "Form Column", "Form Field Parameter", "Grid Column Parameter" };
            //---------------Assert preconditions---------------
            //---------------Execute Test ----------------------
            IUIViewAddElementForm form = new UIViewAddElementForm(GetControlFactory(), null);
            //---------------Test Result -----------------------
            Assert.AreEqual(expectedContents.Length, form.ElementsComboBox.Items.Count);
            foreach (var item in form.ElementsComboBox.Items)
            {
                Assert.Contains(item.ToString(), expectedContents);
            }
        }

        [Test]
        public void Test_ChooseColumn_CreatesFields()
        {
            //---------------Set up test pack-------------------
            IUIViewAddElementForm form = new UIViewAddElementForm(GetControlFactory(), null);
            //---------------Assert preconditions---------------
            Assert.AreEqual(0, form.FieldsPanel.Controls.Count);
            //---------------Execute Test ----------------------
            form.ElementsComboBox.SelectedItem = "Grid Column";
            //---------------Test Result -----------------------
            IControlCollection controls = form.FieldsPanel.Controls[0].Controls;
            Assert.LessOrEqual(1, controls.Count);
        }

        [Test]
        public void Test_ChooseField_CreatesFields()
        {
            //---------------Set up test pack-------------------
            IUIViewAddElementForm form = new UIViewAddElementForm(GetControlFactory(), null);
            //---------------Assert preconditions---------------
            Assert.AreEqual(0, form.FieldsPanel.Controls.Count);
            //---------------Execute Test ----------------------
            form.ElementsComboBox.SelectedItem = "Form Field";
            //---------------Test Result -----------------------
            IControlCollection controls = form.FieldsPanel.Controls[0].Controls;
            Assert.LessOrEqual(1, controls.Count);
        }

        [Test]
        public void Test_ChooseGrid_CreatesFields()
        {
            //---------------Set up test pack-------------------
            IUIViewAddElementForm form = new UIViewAddElementForm(GetControlFactory(), null);
            //---------------Assert preconditions---------------
            Assert.AreEqual(0, form.FieldsPanel.Controls.Count);
            //---------------Execute Test ----------------------
            form.ElementsComboBox.SelectedItem = "Grid";
            //---------------Test Result -----------------------
            IControlCollection controls = form.FieldsPanel.Controls[0].Controls;
            Assert.LessOrEqual(1, controls.Count);
        }

        [Test]
        public void Test_ChooseForm_CreatesFields()
        {
            //---------------Set up test pack-------------------
            IUIViewAddElementForm form = new UIViewAddElementForm(GetControlFactory(), null);
            //---------------Assert preconditions---------------
            Assert.AreEqual(0, form.FieldsPanel.Controls.Count);
            //---------------Execute Test ----------------------
            form.ElementsComboBox.SelectedItem = "Form";
            //---------------Test Result -----------------------
            IControlCollection controls = form.FieldsPanel.Controls[0].Controls;
            Assert.LessOrEqual(1, controls.Count);
        }

        [Test]
        public void Test_ChooseFilter_CreatesFields()
        {
            //---------------Set up test pack-------------------
            IUIViewAddElementForm form = new UIViewAddElementForm(GetControlFactory(), null);
            //---------------Assert preconditions---------------
            Assert.AreEqual(0, form.FieldsPanel.Controls.Count);
            //---------------Execute Test ----------------------
            form.ElementsComboBox.SelectedItem = "Grid Filter";
            //---------------Test Result -----------------------
            IControlCollection controls = form.FieldsPanel.Controls[0].Controls;
            Assert.LessOrEqual(1, controls.Count);
        }

        [Test]
        public void Test_ChooseFilterProp_CreatesFields()
        {
            //---------------Set up test pack-------------------
            IUIViewAddElementForm form = new UIViewAddElementForm(GetControlFactory(), null);
            //---------------Assert preconditions---------------
            Assert.AreEqual(0, form.FieldsPanel.Controls.Count);
            //---------------Execute Test ----------------------
            form.ElementsComboBox.SelectedItem = "Grid Filter Property";
            //---------------Test Result -----------------------
            IControlCollection controls = form.FieldsPanel.Controls[0].Controls;
            Assert.LessOrEqual(1, controls.Count);
        }

        [Test]
        public void Test_ChooseUI_CreatesFields()
        {
            //---------------Set up test pack-------------------
            IUIViewAddElementForm form = new UIViewAddElementForm(GetControlFactory(), null);
            //---------------Assert preconditions---------------
            Assert.AreEqual(0, form.FieldsPanel.Controls.Count);
            //---------------Execute Test ----------------------
            form.ElementsComboBox.SelectedItem = "UI";
            //---------------Test Result -----------------------
            IControlCollection controls = form.FieldsPanel.Controls[0].Controls;
            Assert.LessOrEqual(1, controls.Count);
        }

        [Test]
        public void Test_ChooseFormFieldParameter_CreatesFields()
        {
            //---------------Set up test pack-------------------
            IUIViewAddElementForm form = new UIViewAddElementForm(GetControlFactory(), null);
            //---------------Assert preconditions---------------
            Assert.AreEqual(0, form.FieldsPanel.Controls.Count);
            //---------------Execute Test ----------------------
            form.ElementsComboBox.SelectedItem = "Form Field Parameter";
            //---------------Test Result -----------------------
            IControlCollection controls = form.FieldsPanel.Controls[0].Controls;
            Assert.LessOrEqual(1, controls.Count);
        }

        [Test]
        public void Test_ChooseGridColumnParameter_CreatesFields()
        {
            //---------------Set up test pack-------------------
            IUIViewAddElementForm form = new UIViewAddElementForm(GetControlFactory(), null);
            //---------------Assert preconditions---------------
            Assert.AreEqual(0, form.FieldsPanel.Controls.Count);
            //---------------Execute Test ----------------------
            form.ElementsComboBox.SelectedItem = "Grid Column Parameter";
            //---------------Test Result -----------------------
            IControlCollection controls = form.FieldsPanel.Controls[0].Controls;
            Assert.LessOrEqual(1, controls.Count);
        }

        [Test]
        public void Test_ChooseColumn_CreatesBO()
        {
            //---------------Set up test pack-------------------
            IUIViewAddElementForm form = new UIViewAddElementForm(GetControlFactory(), null);
            //---------------Assert preconditions---------------
            Assert.AreEqual(0, form.FieldsPanel.Controls.Count);
            //---------------Execute Test ----------------------
            form.ElementsComboBox.SelectedItem = "Grid Column";
            //---------------Test Result -----------------------
            Assert.IsNotNull(form.BusinessObject);
            Assert.AreEqual(typeof(UIGridColumnInfo), form.BusinessObject.GetType());
            Assert.AreSame(form.BusinessObject, form.PanelInfo.BusinessObject);
        }

        [Test]
        public void Test_ChooseField_CreatesBO()
        {
            //---------------Set up test pack-------------------
            IUIViewAddElementForm form = new UIViewAddElementForm(GetControlFactory(), null);
            //---------------Assert preconditions---------------
            Assert.AreEqual(0, form.FieldsPanel.Controls.Count);
            //---------------Execute Test ----------------------
            form.ElementsComboBox.SelectedItem = "Form Field";
            //---------------Test Result -----------------------
            Assert.IsNotNull(form.BusinessObject);
            Assert.AreEqual(typeof(UIField), form.BusinessObject.GetType());
            Assert.AreSame(form.BusinessObject, form.PanelInfo.BusinessObject);
        }

        [Test]
        public void Test_CancelButton_ReturnsEmptyString()
        {
            //---------------Set up test pack-------------------
            IUIViewAddElementForm form = new UIViewAddElementForm(GetControlFactory(), null);
            //---------------Assert preconditions---------------
            Assert.IsNull(form.XMLToInsert);
            //---------------Execute Test ----------------------
            form.Show();
            form.CancellingButton.PerformClick();
            //---------------Test Result -----------------------
            Assert.AreEqual("", form.XMLToInsert);
        }

        [Test]
        public void Test_AddButton_NoElement_AddDisabled()
        {
            //---------------Set up test pack-------------------
            //---------------Assert preconditions---------------
            //---------------Execute Test ----------------------
            IUIViewAddElementForm form = new UIViewAddElementForm(GetControlFactory(), null);
            //---------------Test Result -----------------------
            Assert.IsFalse(form.AddButton.Enabled);
        }

        [Test]
        public void Test_AddButton_BecomesEnabledWhenElementChosen()
        {
            //---------------Set up test pack-------------------
            IUIViewAddElementForm form = new UIViewAddElementForm(GetControlFactory(), null);
            //---------------Assert preconditions---------------
            Assert.IsFalse(form.AddButton.Enabled);
            //---------------Execute Test ----------------------
            form.ElementsComboBox.SelectedItem = "Form Field";
            //---------------Test Result -----------------------
            Assert.IsTrue(form.AddButton.Enabled);
        }

        [Test]
        public void Test_AddButton_GeneratesXMLForField()
        {
            //---------------Set up test pack-------------------
            IUIViewAddElementForm form = new UIViewAddElementForm(GetControlFactory(), new DMToClassDefsXmlConverter());
            form.ElementsComboBox.SelectedItem = "Form Field";
            string propertyName = TestUtilsShared.GetRandomString();
            bool editableValue = TestUtilsShared.GetRandomBoolean();
            UIField uiField = ((UIField) form.BusinessObject);
            uiField.PropertyName = propertyName;
            uiField.Editable = editableValue;
            //---------------Assert preconditions---------------
            Assert.AreEqual(typeof(UIField), form.BusinessObject.GetType());
            //---------------Execute Test ----------------------
            form.Show();
            form.AddButton.PerformClick();
            //---------------Test Result -----------------------
            StringAssert.Contains(@"property=""" + propertyName + @"""", form.XMLToInsert);
            StringAssert.Contains(@"editable=""" + editableValue.ToString().ToLower() + @"""", form.XMLToInsert);
        }

        [Test]
        public void Test_AddButton_GeneratesXMLForColumn()
        {
            //---------------Set up test pack-------------------
            IUIViewAddElementForm form = new UIViewAddElementForm(GetControlFactory(), new DMToClassDefsXmlConverter());
            form.ElementsComboBox.SelectedItem = "Grid Column";
            string propertyName = TestUtilsShared.GetRandomString();
            bool editableValue = TestUtilsShared.GetRandomBoolean();
            UIGridColumnInfo gridColumnInfo = ((UIGridColumnInfo)form.BusinessObject);
            gridColumnInfo.PropertyName = propertyName;
            gridColumnInfo.Editable = editableValue;
            //---------------Assert preconditions---------------
            Assert.AreEqual(typeof(UIGridColumnInfo), form.BusinessObject.GetType());
            //---------------Execute Test ----------------------
            form.Show();
            form.AddButton.PerformClick();
            //---------------Test Result -----------------------
            StringAssert.Contains(@"property=""" + propertyName + @"""", form.XMLToInsert);
            StringAssert.Contains(@"editable=""" + editableValue.ToString().ToLower() + @"""", form.XMLToInsert);
        }

        [Test]
        public void Test_AddButton_GeneratesXMLForGrid()
        {
            //---------------Set up test pack-------------------
            IUIViewAddElementForm form = new UIViewAddElementForm(GetControlFactory(), new DMToClassDefsXmlConverter());
            form.ElementsComboBox.SelectedItem = "Grid";
            UIGridInfo gridColumnInfo = ((UIGridInfo)form.BusinessObject);
            string sortColumn = TestUtilsShared.GetRandomString();
            gridColumnInfo.SortColumn = sortColumn;
            //---------------Assert preconditions---------------
            Assert.AreEqual(typeof(UIGridInfo), form.BusinessObject.GetType());
            //---------------Execute Test ----------------------
            form.Show();
            form.AddButton.PerformClick();
            //---------------Test Result -----------------------
            StringAssert.Contains(@"sortColumn=""" + sortColumn + @"""", form.XMLToInsert);
        }

        [Test]
        public void Test_AddButton_GeneratesXMLForForm()
        {
            //---------------Set up test pack-------------------
            IUIViewAddElementForm form = new UIViewAddElementForm(GetControlFactory(), new DMToClassDefsXmlConverter());
            form.ElementsComboBox.SelectedItem = "Form";
            UIFormInfo formInfo = ((UIFormInfo)form.BusinessObject);
            string title = TestUtilsShared.GetRandomString();
            formInfo.Title = title;
            //---------------Assert preconditions---------------
            Assert.AreEqual(typeof(UIFormInfo), form.BusinessObject.GetType());
            //---------------Execute Test ----------------------
            form.Show();
            form.AddButton.PerformClick();
            //---------------Test Result -----------------------
            StringAssert.Contains(@"title=""" + title + @"""", form.XMLToInsert);
        }

        [Test]
        public void Test_AddButton_GeneratesXMLForFilter()
        {
            //---------------Set up test pack-------------------
            IUIViewAddElementForm form = new UIViewAddElementForm(GetControlFactory(), new DMToClassDefsXmlConverter());
            form.ElementsComboBox.SelectedItem = "Grid Filter";
            UIGridFilter gridFilter = ((UIGridFilter)form.BusinessObject);
            int columns = TestUtilsShared.GetRandomInt(1,10);
            gridFilter.Columns = columns;
            //---------------Assert preconditions---------------
            Assert.AreEqual(typeof(UIGridFilter), form.BusinessObject.GetType());
            //---------------Execute Test ----------------------
            form.Show();
            form.AddButton.PerformClick();
            //---------------Test Result -----------------------
            StringAssert.Contains(@"columns=""" + columns + @"""", form.XMLToInsert);
        }

        [Test]
        public void Test_AddButton_GeneratesXMLForFilterProp()
        {
            //---------------Set up test pack-------------------
            IUIViewAddElementForm form = new UIViewAddElementForm(GetControlFactory(), new DMToClassDefsXmlConverter());
            form.ElementsComboBox.SelectedItem = "Grid Filter Property";
            UIGridFilterProp gridFilterProp = ((UIGridFilterProp)form.BusinessObject);
            string labelText = TestUtilsShared.GetRandomString();
            gridFilterProp.LabelText = labelText;
            //---------------Assert preconditions---------------
            Assert.AreEqual(typeof(UIGridFilterProp), form.BusinessObject.GetType());
            //---------------Execute Test ----------------------
            form.Show();
            form.AddButton.PerformClick();
            //---------------Test Result -----------------------
            StringAssert.Contains(@"label=""" + labelText + @"""", form.XMLToInsert);
        }

        [Test]
        public void Test_AddButton_GeneratesXMLForUI()
        {
            //---------------Set up test pack-------------------
            IUIViewAddElementForm form = new UIViewAddElementForm(GetControlFactory(), new DMToClassDefsXmlConverter());
            form.ElementsComboBox.SelectedItem = "UI";
            UIView uiView = ((UIView)form.BusinessObject);
            string viewName = TestUtilsShared.GetRandomString();
            uiView.ViewName = viewName;
            //---------------Assert preconditions---------------
            Assert.AreEqual(typeof(UIView), form.BusinessObject.GetType());
            //---------------Execute Test ----------------------
            form.Show();
            form.AddButton.PerformClick();
            //---------------Test Result -----------------------
            StringAssert.Contains(@"name=""" + viewName + @"""", form.XMLToInsert);
        }

        [Test]
        public void Test_AddButton_GeneratesXMLForFormTab()
        {
            //---------------Set up test pack-------------------
            IUIViewAddElementForm form = new UIViewAddElementForm(GetControlFactory(), new DMToClassDefsXmlConverter());
            form.ElementsComboBox.SelectedItem = "Form Tab";
            UITab tab = ((UITab)form.BusinessObject);
            string name = TestUtilsShared.GetRandomString();
            tab.Name = name;
            //---------------Assert preconditions---------------
            Assert.AreEqual(typeof(UITab), form.BusinessObject.GetType());
            //---------------Execute Test ----------------------
            form.Show();
            form.AddButton.PerformClick();
            //---------------Test Result -----------------------
            StringAssert.Contains(@"name=""" + name + @"""", form.XMLToInsert);
        }

        [Test]
        public void Test_AddButton_GeneratesXMLForFormColumn()
        {
            //---------------Set up test pack-------------------
            IUIViewAddElementForm form = new UIViewAddElementForm(GetControlFactory(), new DMToClassDefsXmlConverter());
            form.ElementsComboBox.SelectedItem = "Form Column";
            UIColumnLayout columnLayout = ((UIColumnLayout)form.BusinessObject);
            int width = TestUtilsShared.GetRandomInt();
            columnLayout.Width = width;
            //---------------Assert preconditions---------------
            Assert.AreEqual(typeof(UIColumnLayout), form.BusinessObject.GetType());
            //---------------Execute Test ----------------------
            form.Show();
            form.AddButton.PerformClick();
            //---------------Test Result -----------------------
            StringAssert.Contains(@"width=""" + width + @"""", form.XMLToInsert);
        }

        [Test]
        public void Test_AddButton_GeneratesXMLForFormFieldParameter()
        {
            //---------------Set up test pack-------------------
            IUIViewAddElementForm form = new UIViewAddElementForm(GetControlFactory(), new DMToClassDefsXmlConverter());
            form.ElementsComboBox.SelectedItem = "Form Field Parameter";
            UIParameter parameter = ((UIParameter)form.BusinessObject);
            string name = TestUtilsShared.GetRandomString();
            parameter.Name = name;
            //---------------Assert preconditions---------------
            Assert.AreEqual(typeof(UIParameter), form.BusinessObject.GetType());
            //---------------Execute Test ----------------------
            form.Show();
            form.AddButton.PerformClick();
            //---------------Test Result -----------------------
            StringAssert.Contains(@"name=""" + name + @"""", form.XMLToInsert);
        }
    }
}
